/**
 * Created by ZFL on 2018/2/9.
 */
function PageInit(){
    getStoreHotSales();
    infiniteSroll()
}

//热销单品
var pageNum = 1;
var pageSize =10;
function getStoreHotSales() {
    var obj = {};
    obj.pageNum=pageNum;
    obj.pageSize=pageSize;
    if (com_obj.mallStoreId) {
        obj.storeId=com_obj.mallStoreId;
    } else {
        obj.storeId = $.getStorage("mallStoreId");
    }
    if (com_obj.userId) {
        obj.userId = com_obj.userId;
    } else {
        obj.userId = 0;
    }
    $.ajaxHandler({
            model: function () {
                console.log(obj)
                return obj;
            },
            visibleTime: 100,
            dataType: "json",
            api: 'yg_baijiaplatform/goods/getStoreHotSales',
            success: function (result) {
                //console.log(result);
                if (result.code == 10000) {
                    var data = result.data.list;
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            if (data[i]["goodsImage"].toString().indexOf('http') < 0) {
                                data[i]["goodsImage"] = $.imgApi() + "" + data[i]["goodsImage"];
                            }
                            data[i]["lowestPrice"] = divideNumber(data[i]["lowestPrice"], 100);
                            data[i]["suggestedPrice"] = divideNumber(data[i]["suggestedPrice"], 100);
                            data[i].href='./goods.html?goodsId='+data[i].goodsId+'&mallStoreId='+$.getPara("mallStoreId")+'&userTalentId='+$.getPara("userTalentId")+'';
                        }
                        var source = $("#hotGoods-template").html();
                        var template = Handlebars.compile(source);
                        var dataObj = {};
                        dataObj.data = data;
                        $('.hot-list-box').append(template(dataObj));

                        //如果实际售价大于等于建议价则建议价隐藏
                        for (var j = 0; j < data.length; j++) {
                            if (divideNumber(data[j]["lowestPrice"], 100) - divideNumber(data[j]["suggestedPrice"], 100) >= 0) {
                                $(".r-price-" + data[j].goodsId + "").hide();
                            }
                        }
                    } else {
                        if (pageNum > 1) {
                            //$.toast("没有数据了","text");
                            $("body").destroyInfinite();
                        }
                    }
                } else {
                    $.toast('异常', 'text');
                    //console.log(result);
                }
            }
        }
    );
}


//滚动加载
//滚动加载
loading = false;
function infiniteSroll() {
    $("body").infinite().on("infinite", function () {
        if (loading) return;
        loading = true;
        pageNum++;
        $('.weui-infinite-scroll-box').show();
        setTimeout(function () {
            loading = false;
            $('.weui-infinite-scroll-box').hide();
            getStoreHotSales();
        }, 1500);   //模拟延迟
    });
}